﻿Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Collections
Imports System.Windows.Forms
Imports DataObject
Imports System.Data.SqlClient

Public Class SqlServerChucDanh

    Private oqLBase As New hDBase
    Private oDataSet As New DataSet
    Private oDatatable As New DataTable

    Public Function GetAllChucDanh(ByVal sConnString As String) As DataTable
        oqLBase.Connection(sConnString)
        oDataSet = oqLBase.GetData("Get_All_ChucDanhs", "ChucDanh")
        Return oDataSet.Tables("ChucDanh")
    End Function

    Public Function InsertChucDanh(ByVal sConnString As String, ByVal oChucDanh As ChucDanh) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoChucDanh", SqlDbType.Int), _
                                           New SqlParameter("@MaChucDanh", SqlDbType.VarChar, 10), _
                                           New SqlParameter("@TenChucDanh", SqlDbType.NVarChar, 50), _
                                           New SqlParameter("@DienGiai", SqlDbType.NVarChar, 255), _
                                           New SqlParameter("@HoatDong", SqlDbType.Bit)}
        SqlParam(0).Direction = ParameterDirection.Output
        SqlParam(1).Value = oChucDanh.MaChucDanh
        SqlParam(2).Value = oChucDanh.TenChucDanh
        SqlParam(3).Value = oChucDanh.DienGiai
        SqlParam(4).Value = oChucDanh.HoatDong
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Insert_ChucDanh", "ChucDanh", SqlParam)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Public Function UpdateChucDanh(ByVal sConnString As String, ByVal oChucDanh As ChucDanh) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoChucDanh", SqlDbType.Int), _
                                                New SqlParameter("@MaChucDanh", SqlDbType.VarChar, 10), _
                                                New SqlParameter("@TenChucDanh", SqlDbType.NVarChar, 50), _
                                                New SqlParameter("@DienGiai", SqlDbType.NVarChar, 255), _
                                                New SqlParameter("@HoatDong", SqlDbType.Bit)}
        SqlParam(0).Value = oChucDanh.SoChucDanh
        SqlParam(1).Value = oChucDanh.MaChucDanh
        SqlParam(2).Value = oChucDanh.TenChucDanh
        SqlParam(3).Value = oChucDanh.DienGiai
        SqlParam(4).Value = oChucDanh.HoatDong
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Update_ChucDanh", "ChucDanh", SqlParam)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Public Function DeleteChucDanh(ByVal sConnString As String, ByVal SoChucDanh As Integer) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoChucDanh", SqlDbType.Int)}
        SqlParam(0).Value = SoChucDanh
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Delete_ChucDanh", "ChucDanh", SqlParam)
            Return True
        Catch ex As SqlException
            Select Case ex.ErrorCode
                Case -2146232060
                    MessageBox.Show("Không thể xóa dữ liệu này. Chức danh này đang được sử dụng trong phần nghiệp vụ khác", "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return False
            End Select
        End Try
    End Function

End Class
